package com.fenghongzhang.lib_common.activity

import android.R.id.message
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.fenghongzhang.lib_common.BlankViewModel
import com.fenghongzhang.lib_common.BuildConfig
import com.fenghongzhang.lib_common.R
import com.fenghongzhang.lib_common.databinding.CommonActivityWebBinding
import com.fenghongzhang.lib_common.utils.ARouterUtils
import com.fenghongzhang.lib_common.utils.Router
import com.fenghongzhang.lib_core.base.BaseToolbarActivity


@Route(path = Router.Ui.CommonWebActivity)
class CommonWebActivity : BaseToolbarActivity<BlankViewModel, CommonActivityWebBinding>() {

    override val mViewModel: BlankViewModel by viewModels()
    override val mLayoutResId: Int = R.layout.common_activity_web

    companion object {
        private var mTitle = ""
        private var url = ""
        private var richText = ""
        private var userWebTitle = false
        private var jump4pay = false
        private var exitBack = false
        private var fullScreen = false

        /** 用户协议 */
        fun jumpUserAgreement(title: String = "用户协议") {
            this.mTitle = title
            this.url = "${BuildConfig.H5_HOST}mobile/agreement?type=2"
            this.fullScreen = false
            this.exitBack = false
            this.userWebTitle = false
            //JLogUtil.d("CommonWebActivity called with url = $url")
            ARouterUtils.jump(Router.Ui.CommonWebActivity)
        }
    }

    override fun initListener() {

    }

    override fun initData() {
        mBinding.webView.settings.apply {
            javaScriptEnabled = true
            loadWithOverviewMode = true
            useWideViewPort = true
            setSupportZoom(true)
        }
        // 添加JS接口
        mBinding.webView.addJavascriptInterface(WebAppInterface(this), "AndroidInterface")

        mBinding.webView.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                //加载进度条
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                //结束进度条
            }
        }

        mBinding.webView.loadUrl("file:///android_asset/demo.html")

        mBinding.webView.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                //加载进度条
                println(newProgress)
            }
        }

        //点击后退
        if(mBinding.webView.canGoBack()){
            mBinding.webView.goBack()
        }

        if(mBinding.webView.canGoForward()){
            mBinding.webView.goForward()
        }

        click()
    }


    // 定义JS接口类
    class WebAppInterface(private val context: Context) {
        @JavascriptInterface
        fun showToast(message: String) {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
        }
    }

    fun click(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mBinding.webView.evaluateJavascript("updateMessage('" + message + "')") { value ->
                // 这里可以处理JS的返回值
                Log.d("JS_RETURN", "返回值: $value")
            }
        } else {
            mBinding.webView.loadUrl("javascript:updateMessage('" + message + "')")
        }
    }

}